﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_ORDER.BizLogic;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_ORDER.OrderObject.UserControls;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using MakerHelper = ERP_RORZE_ORDER.BizLogic.MakerHelper;
using Utilities = ERP_RORZE_TOOL.Common.Utilities;

namespace ERP_RORZE_ORDER.OrderObject.Forms
{
    public partial class frm_OrderEdit : DevExpress.XtraEditors.XtraForm
    {
        private ucOrder parent;

        public string OrderID;
        public DateTime Date;
        public string StaffID;
        private LogUtils log;
        public int Approved;

        public frm_OrderEdit(ucOrder ucP)
        {
            InitializeComponent();
            parent = ucP;
            this.CancelButton = this.btCancel;
            log = new LogUtils();
            this.ActiveControl = txtOrderID;
        }

        private void frm_ProductAdd_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            OrderHelper helper = new OrderHelper(new Order());
            if (GetData(helper.ValueObject))
            {

                if (helper.Update() > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.F2Order.Orders;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "OrderID: " + helper.ValueObject.TempId + "; Date: " + helper.ValueObject.Date;
                    log.WriteLog();

                    if (Utilities.MessageConfirmationYesNo(OrderConstants.Message.UpdateSuccessAndQuestion))
                    {

                        txtOrderID.Focus();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(OrderConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        #region Private Method
        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private bool GetData(Order o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(txtOrderID.Text.Trim()))
            {
                MessageBox.Show(OrderConstants.Message.Order.OrderIDCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(dateDate.EditValue))
            {
                MessageBox.Show(OrderConstants.Message.Order.DateCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbStaff.SelectedValue) || cmbStaff.SelectedItem == cmbStaff.Items[0])
            {
                MessageBox.Show(OrderConstants.Message.Order.DateCantEmpty, OrderConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            #endregion

            o.TempId = txtOrderID.Text;
            o.OrderID = OrderID;
            o.Date = dateDate.DateTime.Date;
            o.StaffID = cmbStaff.SelectedValue.ToString();
            o.Approved = (checkApproved.Checked) ? 1 : 0;

            return true;
        }

        private void BindData()
        {
            txtOrderID.Text = OrderID;
            dateDate.EditValue = Date;
            BindDataStaff();
            checkApproved.Checked = (Approved > 0) ? true : false;
        }

        private void BindDataStaff()
        {
            StaffHelper helper = new StaffHelper(new Staff());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            row[ToolConstants.Entities.Staff.FieldName.StaffName] = string.Empty;
            row[ToolConstants.Entities.Staff.FieldName.StaffID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbStaff.DataSource = dt;
            cmbStaff.DisplayMember = ToolConstants.Entities.Staff.FieldName.FullName;
            cmbStaff.ValueMember = ToolConstants.Entities.Staff.FieldName.StaffID;

            cmbStaff.SelectedValue = StaffID;
            if (cmbStaff.SelectedItem == null)
            {
                cmbStaff.SelectedItem = cmbStaff.Items[0];
            }

        }

        #endregion

        private void frm_OrderEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.D)
                {
                    this.btEdit.PerformClick();
                }
            }
            else if (e.KeyCode == Keys.End)
            {
                btEdit.Focus();
            }
            else if (e.KeyCode == Keys.Home)
            {
                txtOrderID.Focus();
            }
        }


    }
}